home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / res / MessageStyles / Modern Bubbling.AdiumMessageStyle / Contents / Resources / javascripts / reflection.js next >
Encoding:
Text File  |  2007-10-16  |  4.7 KB  |  171 lines

  1. /**
  2.  * reflection.js v1.6
  3.  *
  4.  * Contributors: Cow http://cow.neondragon.net
  5.  *               Gfx http://www.jroller.com/page/gfx/
  6.  *               Sitharus http://www.sitharus.com
  7.  *               Andreas Linde http://www.andreaslinde.de
  8.  *               Tralala, coder @ http://www.vbulletin.org
  9.  *
  10.  * Freely distributable under MIT-style license.
  11.  */
  12.  
  13. /* From prototype.js */
  14. document.getElementsByClassName = function(className) {
  15.     var children = document.getElementsByTagName('*') || document.all;
  16.     var elements = new Array();
  17.   
  18.     for (var i = 0; i < children.length; i++) {
  19.         var child = children[i];
  20.         var classNames = child.className.split(' ');
  21.         for (var j = 0; j < classNames.length; j++) {
  22.             if (classNames[j] == className) {
  23.                 elements.push(child);
  24.                 break;
  25.             }
  26.         }
  27.     }
  28.     return elements;
  29. }
  30.  
  31. var Reflection = {
  32.     defaultHeight : 0.45,
  33.     defaultOpacity: 0.4,
  34.     
  35.     add: function(image, options) {
  36.         Reflection.remove(image);
  37.         
  38.         doptions = { "height" : Reflection.defaultHeight, "opacity" : Reflection.defaultOpacity }
  39.         if (options) {
  40.             for (var i in doptions) {
  41.                 if (!options[i]) {
  42.                     options[i] = doptions[i];
  43.                 }
  44.             }
  45.         } else {
  46.             options = doptions;
  47.         }
  48.     
  49.         try {
  50.             var d = document.createElement('div');
  51.             var p = image;
  52.             
  53.             var classes = p.className.split(' ');
  54.             var newClasses = '';
  55.             for (j=0;j<classes.length;j++) {
  56.                 if (classes[j] != "reflect") {
  57.                     if (newClasses) {
  58.                         newClasses += ' '
  59.                     }
  60.                     
  61.                     newClasses += classes[j];
  62.                 }
  63.             }
  64.  
  65.             var reflectionHeight = Math.floor(p.height*options['height']);
  66.             var divHeight = Math.floor(p.height*(1+options['height']));
  67.             
  68.             var reflectionWidth = p.width;
  69.             
  70.             if (document.all && !window.opera) {
  71.                 /* Copy original image's classes & styles to div */
  72.                 d.className = newClasses;
  73.                 p.className = 'reflected';
  74.                 
  75.                 d.style.cssText = p.style.cssText;
  76.                 p.style.cssText = 'vertical-align: bottom';
  77.             
  78.                 var reflection = document.createElement('img');
  79.                 reflection.src = p.src;
  80.                 reflection.style.width = reflectionWidth+'px';
  81.                 
  82.                 reflection.style.marginBottom = "-"+(p.height-reflectionHeight)+'px';
  83.                 reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(options['opacity']*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(options['height']*100)+')';
  84.                 
  85.                 d.style.width = reflectionWidth+'px';
  86.                 d.style.height = divHeight+'px';
  87.                 p.parentNode.replaceChild(d, p);
  88.                 
  89.                 d.appendChild(p);
  90.                 d.appendChild(reflection);
  91.             } else {
  92.                 var canvas = document.createElement('canvas');
  93.                 if (canvas.getContext) {
  94.                     /* Copy original image's classes & styles to div */
  95.                     d.className = newClasses;
  96.                     p.className = 'reflected';
  97.                     
  98.                     d.style.cssText = p.style.cssText;
  99.                     p.style.cssText = 'vertical-align: bottom';
  100.             
  101.                     var context = canvas.getContext("2d");
  102.                 
  103.                     canvas.style.height = reflectionHeight+'px';
  104.                     canvas.style.width = reflectionWidth+'px';
  105.                     canvas.height = reflectionHeight;
  106.                     canvas.width = reflectionWidth;
  107.                     
  108.                     d.style.width = reflectionWidth+'px';
  109.                     d.style.height = divHeight+'px';
  110.                     p.parentNode.replaceChild(d, p);
  111.                     
  112.                     d.appendChild(p);
  113.                     d.appendChild(canvas);
  114.                     
  115.                     context.save();
  116.                     
  117.                     context.translate(0,image.height-1);
  118.                     context.scale(1,-1);
  119.                     
  120.                     context.drawImage(image, 0, 0, reflectionWidth, image.height);
  121.     
  122.                     context.restore();
  123.                     
  124.                     context.globalCompositeOperation = "destination-out";
  125.                     var gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
  126.                     
  127.                     gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
  128.                     gradient.addColorStop(0, "rgba(255, 255, 255, "+(1-options['opacity'])+")");
  129.         
  130.                     context.fillStyle = gradient;
  131.                     if (navigator.appVersion.indexOf('WebKit') != -1) {
  132.                         context.fill();
  133.                     } else {
  134.                         context.fillRect(0, 0, reflectionWidth, reflectionHeight*2);
  135.                     }
  136.                 }
  137.             }
  138.         } catch (e) {
  139.         }
  140.     },
  141.     
  142.     remove : function(image) {
  143.         if (image.className == "reflected") {
  144.             image.className = image.parentNode.className;
  145.             image.parentNode.parentNode.replaceChild(image, image.parentNode);
  146.         }
  147.     }
  148. }
  149.  
  150. function addReflections() {
  151.     var rimages = document.getElementsByClassName('reflect');
  152.     for (i=0;i<rimages.length;i++) {
  153.         var rheight = null;
  154.         var ropacity = null;
  155.         
  156.         var classes = rimages[i].className.split(' ');
  157.         for (j=0;j<classes.length;j++) {
  158.             if (classes[j].indexOf("rheight") == 0) {
  159.                 var rheight = classes[j].substring(7)/100;
  160.             } else if (classes[j].indexOf("ropacity") == 0) {
  161.                 var ropacity = classes[j].substring(8)/100;
  162.             }
  163.         }
  164.         
  165.         Reflection.add(rimages[i], { height: rheight, opacity : ropacity});
  166.     }
  167. }
  168.  
  169. var previousOnload = window.onload;
  170. window.onload = function () { if(previousOnload) previousOnload(); addReflections(); }
  171.